'''
调试子界面类， 使用sub_debug.ui生成的布局
手动触发
'''
import enum
import sys
import os
import time
import traceback
from functools import partial

from PyQt5 import QtWidgets

from PyQt5.QtWidgets import QApplication, QHeaderView, QWidget

from ui.client1.sub_ui.new_set import Ui_Form  # 界面控件， QtDesigner生成

from log import logger

import yaml
def read_yaml(file_path):
    with open(file_path, 'r') as file:
        config = yaml.safe_load(file)
    return config


def write_yaml(data, file_path):
    with open(file_path, 'w') as file:
        yaml.safe_dump(data, file)


# sys.path.append(r'D:\code\git\zxc\fuba_\project_format_fubatianxian')
class NewSetWidget(QWidget, Ui_Form):
    '''

    usage:

    '''

    def __init__(self, parent=None, main_ui_handle=None):
        super(NewSetWidget, self).__init__(parent)

        self.setupUi(self)
        self.main_ui_handle = main_ui_handle

        # self.param_path = f"{self.main_ui_handle.ui_config['root_path']}/config/param.yaml"
        self.param_path = f"config/param.yaml"
        # 额外样式
        self.setStyleSheet("""
                QPushButton {
                    min-width: 80px;
                    padding: 5px;
                    border-radius: 3px;
                    color: white;
                    background-color: dodgerblue;
                }
                QPushButton:hover {
                    background-color: darkblue;
                }

                /* 定义 QGroupBox 样式 */
                QGroupBox {
                    /*font-size: 14px;*/
                    font-weight: bold;
                    border: 1px solid #ccc;
                    border-radius: 8px; /* 使用像素值代替分数 */
                    padding: 1em;
                    margin-top: 1em;
                    background-color: #f5f5f5;
                }

                /* 设置标题样式 */
                QGroupBox::title {
                    subcontrol-origin: margin;
                    /*subcontrol-position: top center;*/
                    padding: 0 1em;
                    color: #333;
                    background-color: qlineargradient(x1: 0, y1: 0, x2: 0, y2: 1,
                                                    stop: 0 #f5f5f5, stop: 1 #e5e5e5);
                    border-bottom: 1px solid #ccc;
                    border-top-left-radius: 8px;
                    border-top-right-radius: 8px;
                }

                /* 输入框样式 */
                QLineEdit {
                    border: 1px solid #ccc; /* 边框颜色 */
                    border-radius: 4px; /* 边框圆角 */
                    background-color: #fff; /* 背景颜色 */
                }
                /* 输入框焦点样式 */
                QLineEdit:focus {
                    border-color: #80bfff; /* 焦点边框颜色 */
                }
            """)
        self.LineEdit_2.setReadOnly(True)
        # self.LineEdit.textChanged.connect(self.set_delete_days_param_slot)
        self.pushButton.clicked.connect(self.set_delete_days_param_slot)

        param_dict = read_yaml(self.param_path)
        if param_dict.get('delete_days'):
            self.LineEdit.setText(str(param_dict['delete_days']))


    def set_delete_days_param_slot(self):
        print('xx')
        try:

            min_days = int(self.LineEdit_2.text())
            delete_days = int(self.LineEdit.text()) # 删除n天以前的
            if delete_days < min_days:
                if self.main_ui_handle:
                    self.main_ui_handle.loginfo(f'删除天数应大于{min_days}')
                return
            param_dict = read_yaml(self.param_path)
            param_dict['delete_days'] = delete_days
            write_yaml(param_dict, self.param_path)
            if self.main_ui_handle:
                self.main_ui_handle.loginfo(f'删除天数设置为{delete_days}')
        except:
            print(traceback.format_exc())




if __name__ == '__main__':
    try:
        app = QtWidgets.QApplication(sys.argv)
        mainWnd = NewSetWidget()
        # ui = Ui_MainWindow()
        # ui.setupUi(mainWnd)
        mainWnd.show()
        sys.exit(app.exec())
    except:
        print(traceback.format_exc())
        logger.error(traceback.format_exc())